﻿using System;
using System.Windows.Forms;
using System.Data;
using Saturn.App.Common;
using Saturn.App.FormOper;
using System.Collections.Generic;

namespace Saturn.App
{
    public partial class FormWx : Form
    {
        string billcode = "";
        string productline = "";
        string equipmentcode = "";
        string equipmentname = "";
        string equipmentlocation = "";
        string equipmentmodel = "";
        //string maintainproject = "";
        //string maintainpcontent = "";
        string faultsymbol = "";
        string starttime = "";
        string endtime = "";
        string daytime = "";
        string username = "";
        //string maintaintype = "";

        SaturnWebReference.SaturnWebService soap = new Saturn.App.SaturnWebReference.SaturnWebService();

        public FormWx()
        {
            InitializeComponent();
        }         
        private void FrmInventory_Load(object sender, EventArgs e)
        {
            //txtProduct.Focus();
            //GetScanData.CallScan += SetTxtBoxInfo;
            //BindData();
        } 
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }  
        private void btnStart_Click(object sender, EventArgs e)
        {
            btn_Finish.Enabled = true;
            //记录开始保养时的时间
            starttime = DateTime.Now.ToString();
            daytime = DateTime.Now.ToString("yyyyMMdd");
            //生成保养单号 设备编号+日期+类型+序列号
            billcode = CommonCfg.CreateBillCode(daytime, equipmentcode, EnumClass.EquipmentType.Wx);
            lbl_billcode.Text = billcode;
        }       

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (this.lbx_source.SelectedIndex >= 0)
            {
                if (!this.lbx_selected.Items.Contains(this.lbx_source.SelectedItem))
                {
                    this.lbx_selected.Items.Add(this.lbx_source.SelectedItem);
                }
                else
                {
                    MessageBox.Show("无法重复添加");
                }
            }
            else
            {
                MessageBox.Show("请选择要添加的故障现象");
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (this.lbx_selected.SelectedIndex >= 0)
            {
                this.lbx_selected.Items.Remove(this.lbx_selected.SelectedItem);
            }
            else
            {
                MessageBox.Show("请选择要删除的故障现象");
            }
        }

        private void txt_equipmentcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (char)13)
            {
                FormInit();
                DataSet ds = new DataSet();
                ds = soap.GetEquipmentDetail(txt_equipmentcode.Text.ToString());
                //设备编号
                equipmentcode = txt_equipmentcode.Text;
                //设备名称
                equipmentname = ds.Tables[0].Rows[0].ItemArray[3].ToString();
                lbl_equipmentname.Text = equipmentname;
                //设备位置
                equipmentlocation = ds.Tables[0].Rows[0].ItemArray[2].ToString();
                lbl_equipmentlocation.Text = equipmentlocation;
                //设备型号
                equipmentmodel = ds.Tables[0].Rows[0].ItemArray[10].ToString();
                lbl_equipmentmodel.Text=equipmentmodel;
                //故障现象
                lbx_source.DataSource = soap.GetFaultPhenomenon().Tables[0];
                lbx_source.DisplayMember = "FaultPhenomenon";
                lbx_source.ValueMember = "id";

                lbx_selected.DisplayMember = "FaultPhenomenon";
                lbx_selected.ValueMember = "id";                
            }
        }
        
        private void FormInit()
        {
            txt_equipmentcode.Enabled = true;
            lbx_selected.Enabled = true;
            lbx_source.Enabled = true;
            //btn_Finish.Enabled = true;
            //btnStart.Enabled = true;
            btn_add.Enabled = true;
            btn_del.Enabled = true;
            
        }

        private void btn_Finish_Click(object sender, EventArgs e)
        {
            productline = CommonCfg.ProductLine;
            endtime = DateTime.Now.ToString();
            username = CommonCfg.Userrealname;
            if (lbx_selected.Items.Count > 0)
            {
                faultsymbol = "";
                for (int i = 0; i < lbx_selected.Items.Count; i++)
                {
                    DataRowView item = (DataRowView)this.lbx_selected.Items[i];
                    faultsymbol = faultsymbol + "," + item.Row.ItemArray[1].ToString();
                }
                faultsymbol = faultsymbol.Substring(1);               
            }
            else
            {
                MessageBox.Show("未选择故障现象");
            }
            if (!String.IsNullOrEmpty(billcode) & !String.IsNullOrEmpty(productline) & !String.IsNullOrEmpty(equipmentcode) & !String.IsNullOrEmpty(equipmentname) & !String.IsNullOrEmpty(starttime) & !String.IsNullOrEmpty(endtime) & !String.IsNullOrEmpty(username) & !String.IsNullOrEmpty(faultsymbol) & !String.IsNullOrEmpty(equipmentmodel) & !String.IsNullOrEmpty(equipmentlocation))
            {
                 //调用接口保存维修记录
                bool res= soap.AddRepairDetail(billcode, productline, equipmentcode, equipmentname, starttime, endtime, username, faultsymbol, equipmentmodel, equipmentlocation);
                if (res)
                {
                    MessageBox.Show("维修记录已提交");
                }
                else
                {
                    MessageBox.Show("维修记录提交失败，请检查网络");
                }
            }
            else
            {
                MessageBox.Show("请完善设备维修信息");
            }
        }

    }
}